//
//  BRLotteryViewController.m
//  MyBaseProject
//
//  Created by 任波 on 2017/9/12.
//  Copyright © 2017年 任波. All rights reserved.
//

#import "BRLotteryViewController.h"
#import <WebKit/WebKit.h>

@interface BRLotteryViewController ()<WKNavigationDelegate>
// 返回按钮
@property (nonatomic, strong) UIButton *backBtn;
// 关闭按钮
@property (nonatomic, strong) UIButton *closeBtn;
// webView
@property (nonatomic, strong) WKWebView *webView;

@end

@implementation BRLotteryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    [self initUI];
    [self loadData];
    [MBProgressHUD showLoading:@"加载中..."];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void)initUI {
    [self setupNav];
    // webView
    [self.view addSubview:self.webView];
}

#pragma mark - 设置导航栏
- (void)setupNav {
    // 设置导航栏背景图片
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    bgView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:bgView];
    UIImageView *navImageView = [[UIImageView alloc]initWithFrame:bgView.bounds];
    navImageView.backgroundColor = [UIColor clearColor];
    navImageView.image = [UIImage imageNamed:@"navbar_bg"];
    [bgView addSubview:navImageView];
    // 中间导航栏标题
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:17.0f];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.text = @"开奖";
    [bgView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(bgView.mas_centerX);
        make.centerY.mas_equalTo(bgView.mas_centerY).with.offset(10);
        make.height.mas_equalTo(20);
    }];
    // 返回按钮
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.backgroundColor = [UIColor clearColor];
    backBtn.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    [backBtn setTitle:@"返回" forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"navbar_return"] forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"navbar_return"] forState:UIControlStateHighlighted];
    [backBtn sizeToFit]; // 图片自动适应按钮大小
    [backBtn addTarget:self action:@selector(clickBackButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.centerY.mas_equalTo(self.view.mas_top).with.offset(42);
        make.size.mas_equalTo(CGSizeMake(60, 40));
    }];
    backBtn.hidden = YES;
    self.backBtn = backBtn;
    // 关闭按钮
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    closeBtn.backgroundColor = [UIColor clearColor];
    closeBtn.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    [closeBtn setTitle:@"关闭" forState:UIControlStateNormal];
    [closeBtn sizeToFit]; // 图片自动适应按钮大小
    closeBtn.hidden = YES;
    [closeBtn addTarget:self action:@selector(clickCloseBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backBtn.mas_right).with.offset(5);
        make.centerY.mas_equalTo(self.view.mas_top).with.offset(42);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    self.closeBtn = closeBtn;
}

// 加载URL
- (void)loadData {
    NSURL *url = [NSURL URLWithString:URL_KaiJiang];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

#pragma mark - WKNavigationDelegate
/** 页面开始加载时调用 */
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    NSLog(@"开始加载");
}

// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
    NSLog(@"当内容开始返回时调用");
}

// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    NSLog(@"加载完成");
    NSLog(@"当前页面的title：%@", webView.title); // 当前页面的title
    [MBProgressHUD hideHUD];
}

// 内容加载失败时候调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    NSLog(@"页面加载超时");
    [MBProgressHUD showError:@"网络不给力"];
}

#pragma mark - WKNavigationDelegate - 拦截URL
// 用户点击网页上的链接，需要打开新页面时，将先调用这个方法
// 服务器开始请求的时候调用
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    NSURL *URL = navigationAction.request.URL;
    NSString *currentURL = URL.absoluteString;
    NSLog(@"拦截URL：%@", currentURL);
    if ([currentURL containsString:@"mqq2.info.html"]) {
        self.backBtn.hidden = YES;
        self.closeBtn.hidden = YES;
        self.tabBarController.tabBar.hidden = NO;
        self.webView.frame = CGRectMake(0, NAV_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - NAV_HEIGHT - TABBAR_HEIGHT);
        decisionHandler(WKNavigationActionPolicyAllow); //允许加载
    } else {
        self.backBtn.hidden = NO;
        self.closeBtn.hidden = NO;
        self.tabBarController.tabBar.hidden = YES;
        self.webView.frame = CGRectMake(0, NAV_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - NAV_HEIGHT);
        if ([currentURL containsString:@"mqq2.local.html?vb2ctag=4_2089_3_2834"]) {
            // 取消加载
            decisionHandler(WKNavigationActionPolicyCancel); //取消加载
            [MBProgressHUD showInfo:@"敬请期待..."];
        } else {
            decisionHandler(WKNavigationActionPolicyAllow); //允许加载
        }
    }
}

- (WKWebView *)webView {
    if (!_webView) {
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, NAV_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - NAV_HEIGHT - TABBAR_HEIGHT)];
        _webView.backgroundColor = [UIColor clearColor];
        // 允许左右滑动手势返回
        _webView.allowsBackForwardNavigationGestures = YES;
        _webView.navigationDelegate = self;
    }
    return _webView;
}

//点击返回的方法
- (void)clickBackButton {
    //判断是否有上一层H5页面
    if ([self.webView canGoBack]) {
        //如果有则返回
        [self.webView goBack];
    }
}

- (void)clickCloseBtn {
    // 重新加载
    [self loadData];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)dealloc {
    _webView.navigationDelegate = nil;
}

@end
